﻿package net.wangit.dev.settings;

import net.wangit.MapList;
import net.wangit.Path;
import net.wangit.context.ActionContext;
import net.wangit.framework.config.BooleanString;
import net.wangit.framework.config.ConsoleSettings;
import net.wangit.support.Action;
import net.wangit.support.MapListFactory;
import net.wangit.util.FileUtil;
import net.wangit.util.XMLUtil;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;


public class ConsoleSettingsHelper implements MapListFactory, Action {

	public ConsoleSettingsHelper() {
	}

	@Override
	public ActionContext execute(ActionContext ac) throws Exception {
		String path = Path.getConfigPath(ConsoleSettings.FILE_NAME);
		if (FileUtil.exists(path)) {
			FileUtil.delete(path);
		}
		Document doc = DocumentHelper.createDocument();
		Element el = doc.addElement("project");
		el.addElement("isAutoSaveVersion").setText(
				String.valueOf(BooleanString.TRUE.equals(ac
						.getRequestParameter("consolesettings.attr1"))));
		el.addElement("componentLabelRule").setText(
				ac.getRequestParameter("consolesettings.attr2"));
		XMLUtil.writeUTF8(doc, path);
		ConsoleSettings.instance().load();
		return ac;
	}

	@Override
	public MapList getMapList() {
		MapList list = new MapList();
		ConsoleSettings cons = ConsoleSettings.instance();
		list.put(0, "attr1", cons.isAutoSaveVersion() ? BooleanString.TRUE
				: BooleanString.FALSE);
		list.put(0, "attr2", cons.getComponentLabelRule());
		return list;
	}

	/**
	 * 恢复默信设置
	 * 
	 * @param ac
	 * @return
	 * @throws Exception
	 */
	public ActionContext restore(ActionContext ac) throws Exception {
		String path = Path.getConfigPath(ConsoleSettings.FILE_NAME);
		if (FileUtil.exists(path)) {
			FileUtil.delete(path);
		}
		ConsoleSettings.instance().load();
		return ac;
	}
}
